<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home/welcome' }"
        >首页</el-breadcrumb-item
      >
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索input框 -->
      <el-row class="searth">
        <el-col :span="8">
          <el-input placeholder="请输入订单编号" v-model="queryOrder.query" clearable @clear="empty" @keyup.enter.native="query">
            <!-- 搜索announce -->
            <el-button slot="append" icon="el-icon-search" @click="queryBtn"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 订单表格 -->
      <el-table :data="orderList" stripe border>
        <el-table-column type="index" label="#">
        </el-table-column>
        <el-table-column prop="order_number" label="订单编号">
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格"></el-table-column>
        <el-table-column label="是否支付">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.pay_status === '1'"
              >已付款</el-tag
            >
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"></el-table-column>
        <el-table-column prop="create_time" label="下单时间">
          <template slot-scope="scope">
            {{ scope.row.create_time | time }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              title="修改地址"
              @click="editBtn(scope.row)"
            ></el-button>
            <el-button
              type="success"
              icon="el-icon-location-information"
              circle
              title="物流状态"
              @click="logisticsBtn"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="orderListChange"
        @current-change="orderListCurrentChange"
        :current-page="queryOrder.pagenum"
        :page-sizes="[1, 5, 10, 15]"
        :page-size="queryOrder.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 修改地址对话框 -->
    <el-dialog
      title="修改地址"
      :visible.sync="editVisible"
      width="50%"
      @close="resetEdit"
    >
      <!-- 效验表单 -->
      <el-form :model="editForm" :rules="editRules" ref="editRuleFormRef">
        <el-form-item label="省市区/县" prop="city">
          <el-cascader
            :options="cityData"
            :props="{ expandTrigger: 'hover' }"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="site">
          <el-input v-model="editForm.site"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="editVisible = false">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 展示物流信息对话框 -->
    <el-dialog title="物流信息" :visible.sync="logisticsVisible" width="50%">
      <!-- 时间线区域 -->
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in logisticsList"
          :key="index"
          :timestamp="activity.time"
        >
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import {
  getOrdersListApi,
  getOrderDetailsApi,
  getLogisticsApi
} from '@/api/order.js'
// 导入地区数据
import cityData from '@/utils/citydata.js'
export default {
  name: 'Order',
  data() {
    return {
      // 查询参数对象
      queryOrder: {
        // 查询参数
        query: '',
        // 当前页码
        pagenum: 1,
        // 每页显示条数
        pagesize: 5
      },
      // 总条数
      total: 0,
      // 订单列表
      orderList: [],
      // 控制修改地址对话框显示与隐藏
      editVisible: false,
      // 当前修改的订单信息对象
      editForm: {
        city: [],
        site: ''
      },
      // 修改地址表单效验
      editRules: {
        city: [{ required: true, message: '请输入活动名称', trigger: 'blur' }],
        site: [{ required: true, message: '请输入活动名称', trigger: 'blur' }]
      },
      // 地区资料
      cityData: cityData,
      // 控制物流信息对话框显示与隐藏
      logisticsVisible: false,
      // 物流信息数据的数组
      logisticsList: []
    }
  },
  created() {
    this.getOrdersListApi()
  },
  methods: {
    // 封装订单列表请求
    getOrdersListApi() {
      getOrdersListApi(this.queryOrder).then((res) => {
        if (res.data.meta.status !== 200) {
          return this.$message.error('获取订单列表失败')
        }
        this.$message.success('获取订单列表成功')
        this.orderList = res.data.data.goods
        this.total = res.data.data.total
      })
    },
    // 封装查看订单详情请求
    getOrderDetailsApi(id) {
      getOrderDetailsApi(id)
    },
    // 封装物流信息请求
    getLogisticsApi() {
      getLogisticsApi().then((res) => {
        if (res.data.meta.status !== 200) {
          return this.$message.error('获取物流信息失败')
        }
        this.$message.success('获取物流信息成功')
        this.logisticsList = res.data.data
      })
    },
    // 每页显示条数触发
    orderListChange(pagesize) {
      this.queryOrder.pagesize = pagesize
      this.getOrdersListApi()
    },
    // 当前页码改变触发
    orderListCurrentChange(pagenum) {
      this.queryOrder.pagenum = pagenum
      this.getOrdersListApi()
    },
    // 点击修改按钮触发,显示对话框
    editBtn(row) {
      // 查询订单详细资料
      this.getOrderDetailsApi(row.order_id)

      this.editVisible = true
    },
    // 重置修改表单
    resetEdit() {
      this.$refs.editRuleFormRef.resetFields()
    },
    // 点击查询物流按钮触发
    logisticsBtn() {
      // 调用物理信息请求
      this.getLogisticsApi()
      this.logisticsVisible = true
    },
    // 搜索订单按钮
    queryBtn() {
      this.getOrdersListApi()
    },
    // 清空搜索框触发
    empty() {
      this.getOrdersListApi()
    },
    // 搜索框按下enter键触发
    query() {
      this.getOrdersListApi()
    }
  }
}
</script>

<style lang="less" scoped>
.el-card {
  margin-top: 15px;
}
.searth {
  margin-bottom: 15px;
}

.el-cascader {
  width: 100%;
}
</style>
